package com.byedu.focus;

import com.byedu.base.BaseService;
import com.byedu.domain.focus.FocusProduceDO;
import com.byedu.domain.focus.FocusWorkorderDO;
import com.byedu.domain.focus.vo.FocusProjectVO;
import com.byedu.domain.focus.vo.FocusUserVO;
import com.byedu.domain.focus.vo.FocusWorkorderVO;
import com.byedu.domain.focus.vo.enumvos.FocusDepartmentTypeEnum;
import com.byedu.domain.focus.vo.enumvos.FocusDepartmentTypeVO;
import com.byedu.domain.focus.vo.enumvos.FocusStatusTypeEnum;
import com.byedu.domain.focus.vo.enumvos.FocusStatusTypeVO;
import com.byedu.mapper.focus.FocusProduceMapper;
import com.byedu.mapper.focus.FocusProjectMapper;
import com.byedu.mapper.focus.FocusUserMapper;
import com.byedu.mapper.focus.FocusWorkorderMapper;
import com.byedu.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jason on 2017/5/14.
 */
@Service
public class FocusWorkorderService extends BaseService {

    @Autowired
    FocusProjectMapper focusProjectMapper;
    @Autowired
    FocusUserMapper focusUserMapper;
    @Autowired
    FocusWorkorderMapper focusWorkorderMapper;


    public Integer updateFocusWorkorderDObyId(FocusWorkorderDO focusWorkorderDO){
        return focusWorkorderMapper.updateFocusWorkorderDObyId(focusWorkorderDO);
    }




    public List<FocusWorkorderVO> getFocusWorkorderdoVOList(List<FocusProjectVO> focusProjectVOList,List<FocusUserVO> focususerVOList,FocusWorkorderDO searchWork){
//        List<FocusProjectVO>  focusProjectVOList = focusProjectMapper.getFocusProjectDOList();
        List<FocusWorkorderDO> focusWorkorderDOList = focusWorkorderMapper.getFocusWorkorderDOdoList(searchWork);
        List<FocusWorkorderVO> focusWorkorderVOList = new ArrayList<FocusWorkorderVO>();
        for(FocusWorkorderDO focusWorkorderDO : focusWorkorderDOList){
            FocusUserVO focusUserVO = null;
            FocusUserVO fauser= null;
            FocusProjectVO focusProjectVO =null;
            for(FocusUserVO focusUsernVO : focususerVOList){
                if(focusUsernVO.getId().equals(focusWorkorderDO.getUserid())){
                    focusUserVO = focusUsernVO;
                    break;
                }
            }
            for(FocusUserVO focusUsernVO : focususerVOList){
                if(focusUsernVO.getId().equals(focusWorkorderDO.getFauserid())){
                    fauser = focusUsernVO;
                    break;
                }
            }
            for(FocusProjectVO focusProjectnVO: focusProjectVOList){
                if(focusProjectnVO.getId()==focusWorkorderDO.getProjectid()){
                    focusProjectVO = focusProjectnVO;
                    break;
                }
            }
            FocusWorkorderVO focusWorkorderVO = new FocusWorkorderVO( focusWorkorderDO,  focusUserVO,fauser,  focusProjectVO);
            focusWorkorderVOList.add(focusWorkorderVO);
        }

        return focusWorkorderVOList;
    }

    public List<FocusWorkorderVO> getFocusFinishWorkorderVOList(List<FocusProjectVO> focusProjectVOList,List<FocusUserVO> focususerVOList,FocusWorkorderDO searchWork){
//        List<FocusProjectVO>  focusProjectVOList = focusProjectMapper.getFocusProjectDOList();
        List<FocusWorkorderDO> focusWorkorderDOList = focusWorkorderMapper.getFocusFinishWorkorderDOList(searchWork);
        List<FocusWorkorderVO> focusWorkorderVOList = new ArrayList<FocusWorkorderVO>();
        for(FocusWorkorderDO focusWorkorderDO : focusWorkorderDOList){
            FocusUserVO focusUserVO = null;
            FocusUserVO fauser= null;
            FocusProjectVO focusProjectVO =null;
            for(FocusUserVO focusUsernVO : focususerVOList){
                if(focusUsernVO.getId().equals(focusWorkorderDO.getUserid())){
                    focusUserVO = focusUsernVO;
                    break;
                }
            }
            for(FocusUserVO focusUsernVO : focususerVOList){
                if(focusUsernVO.getId().equals(focusWorkorderDO.getFauserid())){
                    fauser = focusUsernVO;
                    break;
                }
            }
            for(FocusProjectVO focusProjectnVO: focusProjectVOList){
                if(focusProjectnVO.getId() == focusWorkorderDO.getProjectid()){
                    focusProjectVO = focusProjectnVO;
                    break;
                }
            }
            FocusWorkorderVO focusWorkorderVO = new FocusWorkorderVO( focusWorkorderDO,  focusUserVO,fauser,  focusProjectVO);
            focusWorkorderVOList.add(focusWorkorderVO);
        }

        return focusWorkorderVOList;
    }




    public List<FocusWorkorderVO> getFocusWorkorderVOList(List<FocusProjectVO> focusProjectVOList,List<FocusUserVO> focususerVOList,FocusWorkorderDO searchWork){
//        List<FocusProjectVO>  focusProjectVOList = focusProjectMapper.getFocusProjectDOList();
        List<FocusWorkorderDO> focusWorkorderDOList = focusWorkorderMapper.getFocusWorkorderDOList(searchWork);
        List<FocusWorkorderVO> focusWorkorderVOList = new ArrayList<FocusWorkorderVO>();
        for(FocusWorkorderDO focusWorkorderDO : focusWorkorderDOList){
            FocusUserVO focusUserVO = null;
            FocusProjectVO focusProjectVO =null;
            FocusUserVO fauser= null;
            for(FocusUserVO focusUsernVO : focususerVOList){
                if(focusUsernVO.getId().equals(focusWorkorderDO.getUserid())){
                    focusUserVO = focusUsernVO;
                    break;
                }
            }
            for(FocusUserVO focusUsernVO : focususerVOList){
                if(focusUsernVO.getId().equals(focusWorkorderDO.getFauserid())){
                    fauser = focusUsernVO;
                    break;
                }
            }
            for(FocusProjectVO focusProjectnVO: focusProjectVOList){
                if(focusProjectnVO.getId()==focusWorkorderDO.getProjectid()){
                    focusProjectVO = focusProjectnVO;
                    break;
                }
            }
            FocusWorkorderVO focusWorkorderVO = new FocusWorkorderVO( focusWorkorderDO,  focusUserVO, fauser, focusProjectVO);
            focusWorkorderVOList.add(focusWorkorderVO);
        }

        return focusWorkorderVOList;
    }


    public Integer addFocusWorkorderDO(FocusWorkorderDO focusWorkorderDO){
        return focusWorkorderMapper.addFocusWorkorderDO(focusWorkorderDO);
    }

    public Integer delFocusWorkorderDO(FocusWorkorderDO focusWorkorderDO){
        return focusWorkorderMapper.delFocusWorkorderDO(focusWorkorderDO);
    }

    public List<FocusWorkorderDO> getFocusWorkorderListbyFocusWorkorderDO(FocusWorkorderDO focusWorkorderDO){
        return focusWorkorderMapper.getFocusWorkorderListbyFocusWorkorderDO(focusWorkorderDO);
    }

    public FocusWorkorderDO getFocusWorkorderDObyId(String id){
        return focusWorkorderMapper.getFocusWorkorderDObyId(id);
    }

    public List<FocusDepartmentTypeVO> getFocusDepartmentTypeVOList() {
        List<FocusDepartmentTypeVO> list = new ArrayList<FocusDepartmentTypeVO>();

        for (FocusDepartmentTypeEnum focusDepartmentTypeEnum : FocusDepartmentTypeEnum.values()) {
            FocusDepartmentTypeVO focusDepartmentTypeVO = new FocusDepartmentTypeVO(focusDepartmentTypeEnum);

            list.add(focusDepartmentTypeVO);
        }
        return list;
    }

//    public List<FocusWorkorderVO> getFocusallWorkorderVOList(List<FocusProjectVO> focusProjectVOList,List<FocusUserVO> focususerVOList,FocusWorkorderDO searchWork){
////        List<FocusProjectVO>  focusProjectVOList = focusProjectMapper.getFocusProjectDOList();
//        List<FocusWorkorderDO> focusWorkorderDOList = focusWorkorderMapper.getFocusWorkorderDOList(searchWork);
//        List<FocusWorkorderVO> focusWorkorderVOList = new ArrayList<FocusWorkorderVO>();
//        for(FocusWorkorderDO focusWorkorderDO : focusWorkorderDOList){
//            FocusUserVO focusUserVO = null;
//            FocusUserVO fauser= null;
//            FocusProjectVO focusProjectVO =null;
//            for(FocusUserVO focusUsernVO : focususerVOList){
//                if(focusUsernVO.getId().equals(focusWorkorderDO.getUserid())){
//                    focusUserVO = focusUsernVO;
//                    break;
//                }
//            }
//            for(FocusUserVO focusUsernVO : focususerVOList){
//                if(focusUsernVO.getId().equals(focusWorkorderDO.getFauserid())){
//                    fauser = focusUsernVO;
//                    break;
//                }
//            }
//            for(FocusProjectVO focusProjectnVO: focusProjectVOList){
//                if(focusProjectnVO.getId()==focusWorkorderDO.getProjectid()){
//                    focusProjectVO = focusProjectnVO;
//                    break;
//                }
//            }
//            FocusWorkorderVO focusWorkorderVO = new FocusWorkorderVO( focusWorkorderDO,  focusUserVO,fauser,  focusProjectVO);
//            focusWorkorderVOList.add(focusWorkorderVO);
//        }
//
//        return focusWorkorderVOList;
//    }

    public List<FocusWorkorderVO> getFocusManageWorkorderVOList(List<FocusProjectVO> focusProjectVOList,List<FocusUserVO> focususerVOList,FocusWorkorderDO searchWork){
//        List<FocusProjectVO>  focusProjectVOList = focusProjectMapper.getFocusProjectDOList();
        List<FocusWorkorderDO> focusWorkorderDOList = focusWorkorderMapper.getFocusManageWorkorderDOList(searchWork);
        List<FocusWorkorderVO> focusWorkorderVOList = new ArrayList<FocusWorkorderVO>();
        for(FocusWorkorderDO focusWorkorderDO : focusWorkorderDOList){
            FocusUserVO focusUserVO = null;
            FocusUserVO fauser= null;
            FocusProjectVO focusProjectVO =null;
            for(FocusUserVO focusUsernVO : focususerVOList){
                if(focusUsernVO.getId().equals(focusWorkorderDO.getUserid())){
                    focusUserVO = focusUsernVO;
                    break;
                }
            }
            for(FocusUserVO focusUsernVO : focususerVOList){
                if(focusUsernVO.getId().equals(focusWorkorderDO.getFauserid())){
                    fauser = focusUsernVO;
                    break;
                }
            }
            for(FocusProjectVO focusProjectnVO: focusProjectVOList){
                if(focusProjectnVO.getId()==focusWorkorderDO.getProjectid()){
                    focusProjectVO = focusProjectnVO;
                    break;
                }
            }
            FocusWorkorderVO focusWorkorderVO = new FocusWorkorderVO( focusWorkorderDO,  focusUserVO,fauser,  focusProjectVO);
            focusWorkorderVOList.add(focusWorkorderVO);
        }

        return focusWorkorderVOList;
    }

    public List<FocusWorkorderVO> getFocusmineWorkorderVOList(List<FocusProjectVO> focusProjectVOList,List<FocusUserVO> focususerVOList,FocusWorkorderDO searchWork,int index){
//        List<FocusProjectVO>  focusProjectVOList = focusProjectMapper.getFocusProjectDOList();
        List<FocusWorkorderDO> focusWorkorderDOList = focusWorkorderMapper.getFocusmineWorkorderDOList(searchWork);
        List<FocusWorkorderVO> focusWorkorderVOList = new ArrayList<FocusWorkorderVO>();
        for(FocusWorkorderDO focusWorkorderDO : focusWorkorderDOList){
            FocusUserVO focusUserVO = null;
            FocusUserVO fauser= null;
            FocusProjectVO focusProjectVO =null;
            for(FocusUserVO focusUsernVO : focususerVOList){
                if(focusUsernVO.getId().equals(focusWorkorderDO.getUserid())){
                    focusUserVO = focusUsernVO;
                    break;
                }
            }
            for(FocusUserVO focusUsernVO : focususerVOList){
                if(focusUsernVO.getId().equals(focusWorkorderDO.getFauserid())){
                    fauser = focusUsernVO;
                    break;
                }
            }
            for(FocusProjectVO focusProjectnVO: focusProjectVOList){
                if(focusProjectnVO.getId()==focusWorkorderDO.getProjectid()){
                    focusProjectVO = focusProjectnVO;
                    break;
                }
            }
            if(index > 0 ) {
                if(DateUtil.isSameMonthWithToday(focusWorkorderDO.getStarttime(), index -1)){
                    FocusWorkorderVO focusWorkorderVO = new FocusWorkorderVO(focusWorkorderDO, focusUserVO, fauser, focusProjectVO);
                    focusWorkorderVOList.add(focusWorkorderVO);
                }
            }else{
                FocusWorkorderVO focusWorkorderVO = new FocusWorkorderVO(focusWorkorderDO, focusUserVO, fauser, focusProjectVO);
                focusWorkorderVOList.add(focusWorkorderVO);
            }

        }

        return focusWorkorderVOList;
    }

    public List<FocusWorkorderVO> getFocusExcutehWorkorderVOList(List<FocusProjectVO> focusProjectVOList,List<FocusUserVO> focususerVOList,FocusWorkorderDO searchWork){
//        List<FocusProjectVO>  focusProjectVOList = focusProjectMapper.getFocusProjectDOList();
        List<FocusWorkorderDO> focusWorkorderDOList = focusWorkorderMapper.getFocusExcutehWorkorderDOList(searchWork);
        List<FocusWorkorderVO> focusWorkorderVOList = new ArrayList<FocusWorkorderVO>();
        for(FocusWorkorderDO focusWorkorderDO : focusWorkorderDOList){
            FocusUserVO focusUserVO = null;
            FocusUserVO fauser= null;
            FocusProjectVO focusProjectVO =null;
            for(FocusUserVO focusUsernVO : focususerVOList){
                if(focusUsernVO.getId().equals(focusWorkorderDO.getUserid())){
                    focusUserVO = focusUsernVO;
                    break;
                }
            }
            for(FocusUserVO focusUsernVO : focususerVOList){
                if(focusUsernVO.getId().equals(focusWorkorderDO.getFauserid())){
                    fauser = focusUsernVO;
                    break;
                }
            }
            for(FocusProjectVO focusProjectnVO: focusProjectVOList){
                if(focusProjectnVO.getId()==focusWorkorderDO.getProjectid()){
                    focusProjectVO = focusProjectnVO;
                    break;
                }
            }
            FocusWorkorderVO focusWorkorderVO = new FocusWorkorderVO( focusWorkorderDO,  focusUserVO,fauser,  focusProjectVO);
            focusWorkorderVOList.add(focusWorkorderVO);
        }

        return focusWorkorderVOList;
    }

}